<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $salt
 * @property integer $create_time
 * @property integer $update_time
 */
class User extends CActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, email', 'required'),
            array('username, password', 'length', 'max' => 128),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'salt' => 'Salt',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('salt', $this->salt, true);
        $criteria->compare('create_time', $this->create_time);
        $criteria->compare('update_time', $this->update_time);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function validatePwd($inputPwd) {
        return md5($inputPwd . $this->salt) === $this->password;
    }

    /**
     * 确定某个值是否已经存在
     * @param type $col 列字段
     * @param type $val 值
     */
    protected function isExist($col, $val) {
        $user = User::model()->find($col . '=:col', array(":col" => $val));
        return !($user === NULL);
    }

    public function isExistUsername($username) {
        return $this->isExist("username", $username);
    }

    public function isExistEmail($email) {
        return $this->isExist("email", $email);
    }

    protected function beforeSave() {
        $this->create_time = $this->update_time = time();
        $this->salt = $this->generateSalt();
        $this->temPwd = $this->password;
        $this->password = md5($this->password.$this->salt);
        return parent::beforeSave();
    }

    private $temPwd;
    
    protected function afterSave() {
        parent::afterSave();
        $identity = new UserIdentity($this->username, $this->temPwd);
        $identity->authenticate();
        Yii::app()->user->login($identity);
    }

    protected function generateSalt(){
        return md5(microtime());
    }
   
}